<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * PENGUIN FrameWork
 * @author hungtd <tdhungit@gmail.com> 0972014011
 * @copyright Tran Dinh Hung 2011
 * 
 * Controller Users 
 * User View
 * 
 * @package PenguinFW
 * @subpackage User
 * @version 1.0.0
 */

class Users extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->lang->load('generate', config_item('language'));
        $this->lang->load('user', config_item('language'));        
    }
    
    public function index()
    {
        //$this->PG_ACL('r');
        
        $data = array(
            'text' => 'Trần Đình Hưng'            
        );
        
        $this->layout->set_title(lang('Welcome'));
        
        $this->parser->parse('index', $data);
    }
    
    /**
     * Trang báo lỗi truy cập trang không cho phép
     */
    public function permission()
    {
        $this->layout->set_title(lang('Not Allow Access'));        
        $this->parser->parse('permission');
    }
    
    /**
     * Register on Site
     */
    public function register()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');        
        
        $this->form_validation->set_rules('username', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
		$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required');
        
        if ($this->form_validation->run() == FALSE)                
            $this->parser->parse('register');
        else {
            $data_users = array(
                'username' => $this->input->post('username'),
                'password' => $this->input->post('password'),
                'email' => $this->input->post('email'),
                'typeid' => $this->input->post('username')
            );
            
            $this->load->model('User');
            $this->User->create($data_users);
            redirect('/users');
        }
    }
    
    /**
     * User Login on Site
     */
    public function login()
    {
        if ($this->session->userdata('user_id')) redirect('/users');
        
        $this->load->helper('form');
        $this->load->library('form_validation');        
        
        $this->form_validation->set_rules('username', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
                
        if ($this->form_validation->run() == TRUE) {
            $this->load->model('User');
            
            $username = $this->input->post('username');
            $password_type = $this->input->post('password');
            
            if ($this->User->login($username, $password_type) == TRUE) {                
                redirect('/users');
            }
        }
        
        $this->parser->parse('login');
    }
    
    /**
     * User thoát ra khỏi hệ thống
     */
    public function logout()
    {
        $this->session->sess_destroy();
        redirect('/users');
    }
}

?>
